package com.itheima;

import java.util.ArrayList;
import java.util.Scanner;

public class domin {
    private static ArrayList<Student> list = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public void addStudent() {
        System.out.println("学生学号");
        String sid = scanner.next();
        System.out.println("学生姓名");
        String name = scanner.next();
        System.out.println("学生年龄");
        int age = scanner.nextInt();
        System.out.println("学生生日");
        String birthday = scanner.next();
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功");
    }


    public void fAndStudent() {
        if (list.size() == 0) {
            System.out.println("您还未添加信息");
            return;

        }
        System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "岁" + "\t" + "生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getSid() + student.getName() + student.getAge() + student.getBirthday());
        }
    }

    public int getindex(String sid) {
        int index = -1;                                   // 假设 不存在 ,索引值都是0开始的 所以-1的意思就是不纯在
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);            //   遍历集合找到Student 生成索引的对象
            if (student.getSid().equals(sid)) {         // 找到集合里的  id 和 录入的id 做比较
                index = i;                              // 如果存在 就把索引值 赋值给 index
                break;
            }
        }
        return index;
    }


    public void deleteStudent() {
        if (list.size() == 0) {
            System.out.println("您还未添加信息");
            return;
        }
        while (true) {
            System.out.println("请输入您要删除的学号");
            String net = scanner.next();
            int getindex = getindex(net);
            if (getindex == -1) {
                System.out.println("不存在");
            } else {
                list.remove(getindex);
                System.out.println("删除成功");
                break;
            }
        }

    }

    public void chakanStudent() {
        if (list.size() == 0) {
            System.out.println("您还未添加信息");
            return;
        }
        String sid;
            System.out.println("学生学号");
            sid = scanner.next();
            int getindex = getindex(sid);
            if (getindex == -1) {
                System.out.println("不存在");
            } else {
                System.out.println("学生姓名");
                String name = scanner.next();
                System.out.println("学生年龄");
                int age = scanner.nextInt();
                System.out.println("学生生日");
                String birthday = scanner.next();
                Student stu = new Student(sid, name, age, birthday);
                list.set(getindex, stu);
                System.out.println("修改成功");

            }
        }

    }

